package com.ifast.oupei.controller;


import java.util.Arrays;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.base.BaseController;
import com.ifast.oupei.domain.ItemsDO;
import com.ifast.oupei.domain.KeywordDO;
import com.ifast.oupei.service.ItemsService;
import com.ifast.oupei.service.KeywordService;
import com.ifast.oupei.vo.KeywordVO;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 分析关键字
 * </pre>
 * <small> 2018-06-11 15:48:45 | Aron</small>
 */
@Controller
@RequestMapping("/oupei/keyword")
public class KeywordController extends BaseController {
	@Autowired
	private KeywordService keywordService;
	@Autowired
	private ItemsService itemsService;
	
	@GetMapping()
	@RequiresPermissions("oupei:keyword:keyword")
	String Keyword(){
	    return "oupei/keyword/keyword";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("oupei:keyword:keyword")
	public Result<Page<KeywordDO>> list(KeywordDO keywordDTO){
		Page<KeywordDO> page = keywordService.Listkeyword(getPage(KeywordDO.class));
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("oupei:keyword:add")
	String add(Model model){
		model.addAttribute("itemsList", itemsService.listAllItems());
	    return "oupei/keyword/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("oupei:keyword:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		KeywordDO keyword = keywordService.selctKeyWordById(id);
		model.addAttribute("keyword", keyword);
	    return "oupei/keyword/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("oupei:keyword:add")
	public Result<String> save( KeywordVO keywordVO){
		KeywordDO keyword = new KeywordDO();
		keyword.setKeyword(keywordVO.getKeyword());
		String itemsName = keywordVO.getItemsName();
		if(keywordVO.getItemsNameText()!=null&&keywordVO.getItemsNameText()!=""){//数据库没有类目信息
			ItemsDO itemsDO = new ItemsDO();
			itemsDO.setItems(keywordVO.getItemsNameText());
			itemsService.insert(itemsDO);
			keyword.setItems(itemsDO.getId());
		}else{
			Integer id=itemsService.selectOneByItemsName(itemsName);
			keyword.setItems(id);
		}
		
		keyword.setItemsName(null);
		keywordService.insert(keyword);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("oupei:keyword:edit")
	public Result<String>  update( KeywordDO keyword){
//		ItemsDO itemsDO =new ItemsDO();
//		itemsDO.setItems(keyword.getItemsName());
//		itemsDO.setId(keyword.getItems());
//		itemsService.updateById(itemsDO);
		
		keyword.setItemsName(null);
		keywordService.updateById(keyword);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("oupei:keyword:remove")
	public Result<String>  remove( Integer id){
		keywordService.deleteById(id);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("oupei:keyword:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] ids){
		keywordService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
